convert_unit

Owner: Duncan

Purpose and Documentation

What is the purpose of this function? Explain what it does, and justify it's place in the program. This section will appear in the Technical Documentation, so try to be complete. The function to convert one mass into another.

Syntax

float convert_unit(float mass, char inputUnit, char outputUnit)

Usage

When and how should this function be called? Use examples where necessary.

Input Arguments

  • float mass.
  • char inputUnit. what does this do?
  • char outputUnit. what does this do?

Output Arguments

  • None. This function does not make changes to any existing values.

Return Value

  • float. This function converts mass as an int into mass as a float.

Settings and Registers

Read

  • None

Write

  • None

Operation

Describe in very general terms how the function fulfills it's purpose. This should be reflected in the code.
The floats are needed for some necessary precision, particularly with the calibration.

Code



#define GRAMS 0
#define OUNCES 1
 
float[][] massConvertLookup = {{1.0, 28.349523125},
                             {0.0352739619, 1.0}};
 
/**
 * Converts a mass from one unit to another.
 * Uses the #define conventions of masses, that is, GRAMS = 0,
 * and OUNCES = 1.
 * Usage example:
 * ounceValue = convertUnit(gramValue, GRAMS, OUNCES);
 * @param mass The input mass
 * @param inputUnit A numerical char representing the input mass type
 * @param outputUnit A numerical char represeting the output mass type
 * @return float The converted mass.
 */
float convert_unit(float mass, char inputUnit, char outputUnit)
{
    return mass * massConvertLookup[inputUnit][outputUnit];
}